package games.ninJump.ui.actions
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import interfaces.IUpdate;

	public class ActionTrans extends Sprite implements IUpdate
	{
		private var m_currentFrame:int;
		private var m_width:Number;
		private var m_height:Number;
		
		private var m_bmd:BitmapData;
		private var m_bmdCopy:BitmapData;
		private var m_bm:Bitmap;
		private var m_des:Object ;
		
		private var m_offset:Dictionary;
		private var m_size:Dictionary;
		private var m_totalFrames:int;
		private var m_animations:Vector.<Object>;
		
		public function ActionTrans()
		{
		}
		
		
		public function destroy():void
		{
			clear();
			m_offset = null;
			m_size = null;
			m_animations = null;
			m_bmd = null;
			m_bmdCopy = null;
			m_bm = null;
			m_des = null;
		}
		
		
		public function update(time:Object = null):void
		{
			if( !m_des)return;
			var t:Number = Number(time)
			var temp:Number = ( Math.floor(t / m_animations[m_currentFrame ].duration * 0.001) % m_totalFrames);
			if(m_currentFrame == temp)return;
			m_currentFrame = temp;
			clear();
			
			var label:String = m_animations[m_currentFrame ].frame;
			m_bmdCopy = new BitmapData(m_width *  m_size[label].x ,m_height *  m_size[label].y ,true,0xFFFFFF);
			m_bmdCopy.lock();
			m_bmdCopy.copyPixels(m_bmd,new Rectangle(m_offset[label].x * m_width ,m_height - m_offset[label].y * m_height - m_height * m_size[label].y, m_width *  m_size[label].x,m_height *  m_size[label].y),new Point(0,0));
			m_bmdCopy.unlock();
			
			m_bm = new Bitmap(m_bmdCopy);
			addChild(m_bm);
		}
		
		public function set bmd(n:BitmapData):void
		{
			if(!m_des)
			{
				m_bmd = n;
				m_bmdCopy = m_bmd;
				m_bm = new Bitmap(m_bmdCopy);
				m_bm.smoothing = true;
				addChild(m_bm);
			}
			if(m_bmd == n)return;
			if(m_bmd)
				m_bmd.dispose();
			m_bmd = n;
			m_width = m_bmd.width  ;
			m_height = m_bmd.height  ;
			clear();
			m_currentFrame = 0;
		}
		
		public function get bmd():BitmapData
		{
			return m_bmd;
		}
		
		public function get des():Object
		{
			return m_des;
		}
		
		public function get bmdCopy():BitmapData
		{
			return m_bmdCopy;
		}
		
		public function set des(obj:Object):void
		{
			if(!obj)return;
			m_des = obj;
			m_offset = new Dictionary(true);
			m_size = new Dictionary(true);
			m_totalFrames = m_des.animations[0].key_frames.length;
			for(var i:int = 0; i < m_des.frames.length; i++)
			{
				m_offset[ m_des.frames[i].name] = m_des.frames[i].offset;
				m_size[ m_des.frames[i].name] = m_des.frames[i].size;
			}
			
			m_animations = new Vector.<Object>();
			for(i = 0; i < m_des.animations[0].key_frames.length; i++)
			{
				m_animations.push(m_des.animations[0].key_frames[i]);
			}
		}
		
		
		private function clear():void
		{
			while(numChildren > 0)
			{
				removeChildAt(0);
			}
			if(m_bmdCopy)
			{
				m_bmdCopy.dispose();
				m_bmdCopy = null;
			}
			
		
		}
		

	}
}